{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Unique Number of Occurrences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: uniqueOccurrences"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #独一无二的出现次数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>arr</code>，请你帮忙统计数组中每个数的出现次数。</p>\n",
    "\n",
    "<p>如果每个数的出现次数都是独一无二的，就返回&nbsp;<code>true</code>；否则返回 <code>false</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>arr = [1,2,2,1,1,3]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>在该数组中，1 出现了 3 次，2 出现了 2 次，3 只出现了 1 次。没有两个数的出现次数相同。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>arr = [1,2]\n",
    "<strong>输出：</strong>false\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>arr = [-3,0,1,-3,1,1,1,-3,10,0]\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= arr.length&nbsp;&lt;= 1000</code></li>\n",
    "\t<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [unique-number-of-occurrences](https://leetcode.cn/problems/unique-number-of-occurrences/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [unique-number-of-occurrences](https://leetcode.cn/problems/unique-number-of-occurrences/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,2,1,1,3]', '[1,2]', '[-3,0,1,-3,1,1,1,-3,10,0]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        counter = Counter(arr)\n",
    "        counter_set = set(counter.values())\n",
    "        return len(counter_set) == len(counter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        jihe=set(arr)\n",
    "        dict1={}\n",
    "        for i in jihe:\n",
    "            dict1[i]=arr.count(i)\n",
    "        return(len(set(dict1.values()))==len(jihe))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        Len = len(arr)\n",
    "        for i in range(Len - 1):\n",
    "            for j in range(i + 1, Len):\n",
    "                if arr[i] > arr[j]:\n",
    "                    t = arr[i]\n",
    "                    arr[i] = arr[j]\n",
    "                    arr[j] = t\n",
    "        n = []\n",
    "        sum = []\n",
    "        start = 0\n",
    "        n .append(arr[0])\n",
    "        for i in range(1, Len):\n",
    "            if arr[i] != arr[start]:\n",
    "                sum.append(i - start)\n",
    "                start = i\n",
    "                n.append(arr[i])\n",
    "        sum.append(Len - 1 - start + 1)\n",
    "\n",
    "        for i in range(len(sum) - 1):\n",
    "            for j in range(i, len(sum)):\n",
    "                if sum[i] > sum[j]:\n",
    "                    t = sum[i]\n",
    "                    sum[i] = sum[j]\n",
    "                    sum[j] =t\n",
    "        f = 1\n",
    "        for i in range(len(sum) - 1):\n",
    "            if sum[i + 1] == sum[i]:\n",
    "                f = 0\n",
    "                break\n",
    "        if f == 1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        arr.sort()\n",
    "        ls=collections.Counter(arr)\n",
    "        return  len(list(ls.values()))==len(set(ls.values()))\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        d=collections.Counter(arr)\n",
    "        return len(d.keys())==len(set(d.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        n = set(arr)\n",
    "        res = []\n",
    "        for i in n:\n",
    "            if arr.count(i) not in res:\n",
    "                res.append(arr.count(i))\n",
    "            else:\n",
    "                return False\n",
    "        else:\n",
    "            return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        dict_1 = {}\n",
    "        for i in arr:\n",
    "            if i not in dict_1:\n",
    "                dict_1[i] = 1\n",
    "            else:\n",
    "                dict_1[i] += 1\n",
    "        dict_2 = {}\n",
    "        for i in dict_1.keys():\n",
    "            if dict_1[i] not in dict_2:\n",
    "                dict_2[dict_1[i]] = 1\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        arr_set = set(arr)\n",
    "        count_list =list()\n",
    "        for i in arr_set:\n",
    "            count_num = arr.count(i)\n",
    "            if count_num in count_list:\n",
    "                return False\n",
    "            count_list.append(count_num)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        arrhash = {}\n",
    "        for i in set(arr):\n",
    "            num = arr.count(i) \n",
    "            if num not in arrhash:\n",
    "                arrhash[num] = i\n",
    "            else:\n",
    "                return False\n",
    "        return  True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        c = Counter(arr)\n",
    "        return len(c) == len(set([c[i] for i in c]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        sum={}\n",
    "        for i in arr:\n",
    "            print(sum.keys())\n",
    "            if i in sum.keys():\n",
    "                sum[i]+=1\n",
    "            else:\n",
    "                sum[i]=1\n",
    "        return len(sum.values())==len(set(sum.values()))\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        aList=[]\n",
    "        counta=[]\n",
    "        for i in range(len(arr)):\n",
    "            a=arr[i]\n",
    "            if a not in aList:\n",
    "                aList.append(a)\n",
    "                k=arr.count(a)\n",
    "                if k not in counta:\n",
    "                    counta.append(k)\n",
    "                else:\n",
    "                    return(False)\n",
    "        return(True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        a={}\n",
    "        b=[]\n",
    "        for i in arr:\n",
    "            if i in a:\n",
    "                a[i]=a[i]+1\n",
    "            else:\n",
    "                a[i]=1\n",
    "        for i in a:\n",
    "            b.append(a[i])\n",
    "        \n",
    "        if len(b)==len(set(b)):\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        n = len(arr)\n",
    "        d = {}\n",
    "        for num in arr:\n",
    "            if num not in d:\n",
    "                d[num] = 1\n",
    "            else:\n",
    "                d[num] += 1\n",
    "        v = d.values()\n",
    "        s = set(v)\n",
    "        return len(v) == len(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for e in arr:\n",
    "            if e in d.keys():\n",
    "                d[e] += 1\n",
    "            else:\n",
    "                d[e] = 0\n",
    "        return len(set(d.values())) == len(d.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        arrDict={} #value:frequency\n",
    "        for num in arr:\n",
    "            if not num in arrDict:\n",
    "                arrDict[num]=1\n",
    "            else: #num in arrDict\n",
    "                arrDict[num]+=1\n",
    "        ans=[]\n",
    "        for key in arrDict:\n",
    "            ans.append(arrDict[key])\n",
    "        ans.sort()\n",
    "        print(ans)\n",
    "        i=0\n",
    "        while i<len(ans)-1 and ans[i]!=ans[i+1]:\n",
    "            i+=1\n",
    "        print(i)\n",
    "        if i==len(ans)-1:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        hash = {}\n",
    "        hash1 = {}\n",
    "        for i in arr:\n",
    "            if i not in hash:\n",
    "                hash[i] = 1\n",
    "            else:\n",
    "                hash[i] = hash[i] + 1\n",
    "            print(hash)\n",
    "        for j in hash.values():\n",
    "            if j not in hash1:\n",
    "                hash1[j] = 1\n",
    "            else:\n",
    "                return(False)\n",
    "        return(True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        dic = {}\n",
    "\n",
    "        for i in arr:\n",
    "            dic[i] = dic.get(i, 0) + 1\n",
    "        \n",
    "        return len(list(dic.values())) == len(set(dic.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        arr.sort()\n",
    "        count = {}\n",
    "        left = 0\n",
    "        right = 0\n",
    "        while right<len(arr):\n",
    "            if arr[left]!=arr[right]:\n",
    "                if (right-left) in count.keys():\n",
    "                    return False\n",
    "                else:\n",
    "                    count[right-left] = 1\n",
    "                    left=right\n",
    "            right+=1\n",
    "        if (right-left) in count.keys():\n",
    "            return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def uniqueOccurrences(self, arr: List[int]) -> bool:\n",
    "        cnt = Counter(arr)\n",
    "        if len(cnt) == len(set(cnt.values())):\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
